const schemaJoi = require('./util/schemaJoi');
const Joi = require('joi');
const service = require('./service/service');


/*
* 添加埋点信息 
* @return data: object 埋点的数据
* */
exports.addUserStat = async (context) => {
  const { cloud, data, mixNick, userNick } = context
  console.log("============================", data)
  try {
    //参数验证
    const schema = Joi.object().keys({
      stat_data: Joi.object().required(),
    });
    await schemaJoi(schema, data);
    let paramsData = data.stat_data;
    paramsData.userId = mixNick;
    return await service.addUserStat(JSON.stringify(paramsData), cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + e };
  }
};

/**
 * 上传图片
 */
exports.uploadFile = async (context) => {
  const { cloud } = context;
  let result;
  try {
    result = await cloud.file.uploadFile({
      fileContent: new Buffer("https://resource/apmlc8664a4bb4375a5c68036f7582f1cd39.png"), fileName: "test"
    })
    
  } catch (e) {
    console.log("e", e)
  }
  console.log("uploadFileResult", result);
  return result;
};
exports.getTempFileURL = async (context) => {
  const cloud = context.cloud;
  let fileId = 'cloud://C2D4FD0AAA7B6B4D5107825934B546FB/test';
  let result;
  try {
    result = await cloud.file.getTempFileURL({ fileId })
  } catch (e) {
    console.log("e", e)
  }
  console.log("getTempFileURLResult", result);
  return result;
};

